import React, { useEffect, useState } from 'react';
import { useRequest } from 'ahooks';
import styles from './index.module.less';
import { Last3nBackTest } from '../../../type';
import axios from 'axios';
const useBack = (code: string) => {
  //最大回测
  const [maxBack, setMaxBack] = useState(0);

  const [meinianBack, setMeinianBack] = useState<{
    yi: number;
    er: number;
    san: number;
  }>();

  //获取近三年回测
  const get3nBackTest = async () => {
    try {
      const { data } = await axios.get(
        `https://qieman.com/bmdj/v1/fund/info/${code}/detail?broker=0008`,
      );
      const {
        performance: {
          metricsAnalyzes: [
            {
              metrics: [_q1, _q2, { metricsValueText: yi }],
            },
            {
              metrics: [__q1, __q2, { metricsValueText: er }],
            },
            {
              metrics: [q1, q2, { metricsValueText }],
            },
          ],
        },
      } = data as Last3nBackTest;
     
      //10%再对象中就是10，乘了100
      setMeinianBack({
        yi: Math.abs(parseFloat(String(yi) ?? '')),
        er: Math.abs(parseFloat(String(er) ?? '')),
        san: Math.abs(parseFloat(String(metricsValueText) ?? '')),
      });

      setMaxBack(Math.abs(parseFloat(String(metricsValueText) ?? '')));
    } catch (e) {
      console.log('get3nBackTest', e);
    }
  };

  useEffect(() => {
    get3nBackTest();
  }, [code]);

  return {
    maxBack,
    meinianBack,
  };
};
export default useBack;
